<?php

/** Zend_Controller_Action */
require_once ROOT_PATH . '/library/Soooner/Controller/WebAction.php';
require_once ROOT_PATH . '/library/Common/Sphinx/sphinxapi.php';

class InterfaceController extends Soooner_Web_Controller_Action
{
	public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();    	
    }
    public function initAction(){
    	$now= date('Y-m-d H:i:s',time());
    	$header="<?xml version='1.0' encoding='utf-8' ?>\n";
        include $this->_dzTemplate->getFile( 'web/servertime.html' );
    }
    public function searchAction(){
    	$cl = new SphinxClient();
    	$ipad = $this->_getParam('ipad');
    	$q = $this->_getParam('q');
    	$index = $this->_getParam('index');//页码
    	$index = str_ireplace(".xml","",$index);
    	if(!$index)$index=1;
    	$options = $GLOBALS['application']->getOptions();
    	$pageCount = $options["listcount"];//每页显示的条数
        $mode = SPH_MATCH_ALL;
        $host = "127.0.0.1";
        $port = 3312;
        $spinxindex = "test1";
        $groupby = "";
        $groupsort = "@group desc";
        $filter = "group_id";
        $filtervals = array(0,1);
        if($ipad)
        	$filtervals = array(2,1);
        $distinct = "";
        $sortby = "";
        $limit = 18;
        $ranker = SPH_RANK_PROXIMITY_BM25;
        $select = "";
        $cl->SetFilter($filter,$filtervals);
        $cl->SetServer ( $host, $port );
        $cl->SetConnectTimeout ( 1 );
        $cl->SetArrayResult ( true );
        $cl->SetWeights ( array ( 100, 1 ) );
        $cl->SetMatchMode ( $mode );
        $cl->SetLimits(($index-1)*$limit,$limit);
        $cl->SetRankingMode ( $ranker );
        $res = $cl->Query ( $q, $spinxindex );   
        //print_r($res);   exit;
        $contentidsvod = array();
        $contentidslive = array();
        foreach($res["matches"] as $r){
        	if($r['attrs']['group_id']==1)
        		array_push($contentidsvod,$r['attrs']['contentid']);
        	else
        		array_push($contentidslive,$r['attrs']['contentid']);
        }
        if($contentidsvod){
        	$vodvids = implode(",",$contentidsvod);
        	$modelVod = new Model_Tables_Vod();
        	$vodinfos = $modelVod->getList4Search($vodvids);
        }
        if($contentidslive){
        	$livevids = implode(",",$contentidslive);
        	$modelLive = new Model_Tables_Live();
        	$liveinfos = $modelLive->getList4Search($livevids);
        }
        $count=$res["total"];
        $countInPage = count($res["matches"]);
    	$pageCount = ceil($count/$pageCount);
    	header("Content-Type: text/xml");
    	$header="<?xml version='1.0' encoding='utf-8' ?>\n";
        include $this->_dzTemplate->getFile( 'web/search.html' );
    }
    //返回m3u8文件
    public function ipadAction(){
    	$vod = true;
    	$vid = $this->_getParam('vid');
    	$stime = floor($this->_getParam('s')/10)*10;
    	$etime = ceil($this->_getParam('e')/10)*10;
    	if(!$stime){
    		$now = time()-50;
    		$stime = date('YmdHis',floor($now/10)*10);
    		$etime = date('YmdHis',floor($now/10)*10+30);
    		$vod = false;
    	}
    	$sequence=(strtotime($stime)-strtotime(20100101000000))/10;    	
    	$sequence.="\n";
    	$diff=(strtotime($etime)-strtotime($stime))/10;
    	$videos = array();
    	for($t=0;$t<$diff;$t++){
    		$videos[]=date('YmdHis',strtotime($stime)+10*$t).".ts\n";
    	}
    	header("Content-Type: audio/mpegurl");
    	$header="#EXTM3U\n#EXT-X-ALLOW-CACHE:NO\n#EXT-X-TARGETDURATION:10\n";
        $extinfo="#EXTINF:10,\n";
        $endlist="#EXT-X-ENDLIST\n";
    	if($vod)
    		include $this->_dzTemplate->getFile( 'web/m3u8vod.html' );
    	else
    		include $this->_dzTemplate->getFile( 'web/m3u8.html' );
    }
    public function goAction(){
        $url = $this->_getParam('url');
        $p = explode(".",$url);
        $ext = $p[count($p)-1];
        $modelVodlist = new Model_Tables_Vodlist();
        $rurl = "http://".$_SERVER["SERVER_NAME"].":8000/".$modelVodlist->uu($url)."/tmp.$ext";
        header('Location: '.$rurl);
    }
}